//
//  DealViewController.m
//  project
//
//  Created by 张亚林 on 2017/11/1.
//  Copyright © 2017年 Macmini. All rights reserved.
//

#import "DealViewController.h"


#import "MenuButton.h"
#import "RightMenuButton.h"
#import "DealListTableViewCell.h"
#import "BTCbuyTableViewController.h"
#import "SellBTCTableViewCell.h"
#import "AppDelegate.h"

@interface DealViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    UIImageView *myTempView;
    UIImageView *myTempView1;
}
@property (nonatomic, strong) UIImageView *myView;
@property (nonatomic, strong) UIImageView *myView1;

@property (weak, nonatomic) IBOutlet UIButton *BTCBtn;

@property (weak, nonatomic) IBOutlet UIImageView *imageV1iew;
@property (weak, nonatomic) IBOutlet UIImageView *iamgeView2;

@property (weak, nonatomic) IBOutlet UIButton *buyBTCBtn;
@property (weak, nonatomic) IBOutlet UIButton *sellBTCBtn;
@property (weak, nonatomic) IBOutlet UIView *buyBTCLine;
@property (weak, nonatomic) IBOutlet UIView *sellBTCLine;


@property (weak, nonatomic) IBOutlet UIScrollView *scroll;

@property (weak, nonatomic) IBOutlet UITableView *tableview1;

@property (weak, nonatomic) IBOutlet UITableView *tableview2;

@end

@implementation DealViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //圆角属性
    [self.BTCBtn setCircleButtonRadius:10];
    [self.buyBTCBtn setCircleButtonRadius:10];
    
    self.imageV1iew.image = [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e613",19 ,  [UIColor colorWithRed:187/255.0 green:187/255.0 blue:187/255.0 alpha:1/1.0])];
    self.iamgeView2.image = [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e611", 19, [UIColor colorWithRed:187/255.0 green:187/255.0 blue:187/255.0 alpha:1/1.0])];
    
//    UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    titleButton titleRectForContentRect:CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)
    
    self.tableview1.delegate = self;
    self.tableview1.dataSource = self;
    self.tableview2.delegate = self;
    self.tableview2.dataSource = self;
    
    self.tableview1.tableFooterView = [UIView new];
    self.tableview2.tableFooterView = [UIView new];
    
    
    [self setupNavBar];
    [self creatLeftMune];
    [self creatRightMune];
    
}

- (void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:YES];
    self.myView1.hidden = YES;
    self.menuIsopen1 = NO;
}

//- (void)viewDidDisappear:(BOOL)animated {
//
//    [super viewDidDisappear:YES];
//    self.myView.hidden = YES;
//    self.menuIsopen = NO;
//}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView.tag == 101) {
        DealListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dealListTableViewCell"];
        if (!cell) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"DealListTableViewCell" owner:self options:nil]lastObject];
      
        }
        
        return cell;
    } else {
        
        SellBTCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"sellBTCTableViewCell"];
        if (!cell) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"SellBTCTableViewCell" owner:self options:nil]lastObject];
         
        }
        
        return cell;
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 90;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (tableView.tag == 101) {
        return 3;
    } else {
        return 6;
    }
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if([cell respondsToSelector:@selector(setLayoutMargins:)]){
        [cell setLayoutMargins:UIEdgeInsetsMake(0, 21, 0, 21)];
    }
    
    if([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 21, 0, 21)];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView.tag == 101) {
        BTCbuyTableViewController *btcbuyTVC = [UIStoryboard loadStoryboard:@"BTCbuyTableViewController"];
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:btcbuyTVC animated:YES];
        self.hidesBottomBarWhenPushed = NO;
    }
    
  
}

//配置导航条
- (void)setupNavBar {
    
    self.navigationItem.title = @"交易";
    
    UIButton *leftBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn1 setTitle:@"买BTC" forState:UIControlStateNormal];
    [leftBtn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [leftBtn1 addTarget:self action:@selector(clickLeftBtn1) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *leftBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    //    [leftBtn2 setBackgroundColor:[UIColor blueColor]];
    [leftBtn2 setFrame:CGRectMake(0, 0, 9, 5.5)];
    
    //右侧
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *image = [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e645", 18.5,  [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1/1.0])];
    [ rightBtn setBackgroundImage:image forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(clickRightBtn) forControlEvents:UIControlEventTouchUpInside];
    
   
    
    UIBarButtonItem *leftBarButtonItem1 = [[UIBarButtonItem alloc] initWithCustomView:leftBtn1];
    UIBarButtonItem *leftBarButtonItem2 = [[UIBarButtonItem alloc] initWithCustomView:leftBtn2];
    
    //右侧
    UIBarButtonItem *rightBarButtonItem1 = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
    self.navigationItem.leftBarButtonItems = @[leftBarButtonItem1, leftBarButtonItem2];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem1;
    
}

#pragma mark - leftBarButtonItems点击事件
- (void) clickLeftBtn1 {
    
    NSLog(@"%@", @"clickLeftBtn1");
    if (self.menuIsopen == NO) {
        self.myView.hidden = NO;
        self.menuIsopen = YES;
    }else if (self.menuIsopen == YES){
        self.myView.hidden = YES;
        self.menuIsopen = NO;
    }
}

#pragma mark - 创建左侧菜单
- (void)creatLeftMune {
    
    myTempView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 64+3, 86, 97)];
    myTempView.image = [UIImage imageNamed:@"menuBackground"];
    myTempView.userInteractionEnabled = YES;
    
    MenuButton *button1 = [[MenuButton alloc]init];
    [button1 setTitle:@"买BTC" forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button1.titleLabel.adjustsFontSizeToFitWidth = YES;
    
    UIImage *image1 = [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e602", 12, [UIColor blackColor])];
    [button1 setImage:image1 forState:UIControlStateNormal];
    button1.frame = CGRectMake(0, 0, 120, 40);
    [button1 addTarget:self action:@selector(menuButton1:) forControlEvents:UIControlEventTouchUpInside];
    [myTempView addSubview:button1];
    
    
    MenuButton *button2 = [[MenuButton alloc]init];
    button2.frame = CGRectMake(0, 40, 120, 40);
    button2.titleLabel.adjustsFontSizeToFitWidth = YES;
    [button2 setTitle:@"卖BTC" forState:UIControlStateNormal];
    [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //  [button2 setImage:[UIImage imageNamed:@"Group"] forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(menuButton2:) forControlEvents:UIControlEventTouchUpInside];
    [myTempView addSubview:button2];
/*
//    MenuButton *button3 = [[MenuButton alloc]init];
//    button3.frame = CGRectMake(0, 80, 120, 40);
//    [button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    button3.titleLabel.adjustsFontSizeToFitWidth = YES;
//    [button3 setTitle:@"买ETH" forState:UIControlStateNormal];
//    //   [button3 setImage:[UIImage imageNamed:@"Group"] forState:UIControlStateNormal];
//    [button3 addTarget:self action:@selector(menuButton3:) forControlEvents:UIControlEventTouchUpInside];
//    [myTempView addSubview:button3];
//
//    MenuButton *button4 = [[MenuButton alloc]init];
//    button4.frame = CGRectMake(0, 120, 120, 40);
//    [button4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    button4.titleLabel.adjustsFontSizeToFitWidth = YES;
//    [button4 setTitle:@"卖ETH" forState:UIControlStateNormal];
//    //   [button4 setImage:[UIImage imageNamed:@"Group"] forState:UIControlStateNormal];
//    [button4 addTarget:self action:@selector(menuButton4:) forControlEvents:UIControlEventTouchUpInside];
//    [myTempView addSubview:button4];
 */
    
    self.myView = myTempView;
    myTempView.hidden = YES;
    
    
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window] ;
    [window addSubview:myTempView];
}

//点击事件1
- (void)menuButton1:(UIButton *)btn1 {
    NSLog(@"点击事件1");
    
     self.scroll.contentOffset = CGPointMake(0, 0);
}

//点击事件2
- (void)menuButton2:(UIButton *)btn2 {
    NSLog(@"点击事件2");
    
     self.scroll.contentOffset = CGPointMake(self.view.frame.size.width, 0);
}

#pragma mark - rightBarBottonItem点击事件
- (void)clickRightBtn {
    
   
    NSLog(@"%@", @"clickRightBtn1");
    if (self.menuIsopen1 == NO) {
        self.myView1.hidden = NO;
        self.menuIsopen1 = YES;
    }else if (self.menuIsopen1 == YES){
        self.myView1.hidden = YES;
        self.menuIsopen1 = NO;
    }
    
}

#pragma mark - 创建右侧菜单
- (void)creatRightMune {
    
    myTempView1 = [[UIImageView alloc]initWithFrame:CGRectMake(225, 64+3, 174, 134)];
    myTempView1.image = [UIImage imageNamed:@"menuBackground"];
    myTempView1.userInteractionEnabled = YES;
    
    RightMenuButton *button1 = [[RightMenuButton alloc]init];
    [button1 setTitle:@"挂单买BTC" forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button1.titleLabel.adjustsFontSizeToFitWidth = YES;
    
    UIImage *image1 = [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e602", 12, [UIColor blackColor])];
    [button1 setImage:image1 forState:UIControlStateNormal];
    button1.frame = CGRectMake(0, 0, 150, 30);
    [button1 addTarget:self action:@selector(rightmenuButton1:) forControlEvents:UIControlEventTouchUpInside];
    [myTempView1 addSubview:button1];
    
    
    RightMenuButton *button2 = [[RightMenuButton alloc]init];
    button2.frame = CGRectMake(0, 40, 150, 30);
    button2.titleLabel.adjustsFontSizeToFitWidth = YES;
    [button2 setTitle:@"挂单卖BTC" forState:UIControlStateNormal];
    [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    UIImage *image2 = [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e602", 12, [UIColor blackColor])];
    [button2 setImage:image2 forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(rightmenuButton2:) forControlEvents:UIControlEventTouchUpInside];
    [myTempView1 addSubview:button2];
/*
//    RightMenuButton *button3 = [[RightMenuButton alloc]init];
//    button3.frame = CGRectMake(0, 80, 150, 30);
//    [button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    button3.titleLabel.adjustsFontSizeToFitWidth = YES;
//    [button3 setTitle:@"挂单买ETH" forState:UIControlStateNormal];
//    UIImage *image3 = [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e602", 12, [UIColor blackColor])];
//    [button3 setImage:image3 forState:UIControlStateNormal];
//
//    [button3 addTarget:self action:@selector(rightmenuButton3:) forControlEvents:UIControlEventTouchUpInside];
//    [myTempView1 addSubview:button3];
//
//    RightMenuButton *button4 = [[RightMenuButton alloc]init];
//    button4.frame = CGRectMake(0, 120, 150, 30);
//    [button4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    button4.titleLabel.adjustsFontSizeToFitWidth = YES;
//    [button4 setTitle:@"挂单卖ETH" forState:UIControlStateNormal];
//    UIImage *image4 = [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e602", 12, [UIColor blackColor])];
//    [button4 setImage:image4 forState:UIControlStateNormal];
//
//    [button4 addTarget:self action:@selector(rightmenuButton4:) forControlEvents:UIControlEventTouchUpInside];
//    [myTempView1 addSubview:button4];
*/
    
    self.myView1 = myTempView1;
    myTempView1.hidden = YES;
    
    
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window] ;
    [window addSubview:myTempView1];
}

//点击事件1
- (void)rightmenuButton1:(UIButton *)btn1 {
    NSLog(@"点击事件1");
    ChoosePatternViewController *choosePVC = [ChoosePatternViewController new];
    choosePVC.type = @"buy";
    [self  presentViewController:[[CLNavigationViewController alloc]initWithRootViewController:choosePVC] animated:YES completion:nil];
}

//点击事件2
- (void)rightmenuButton2:(UIButton *)btn2 {
    NSLog(@"点击事件2");
    ChoosePatternViewController *choosePVC = [ChoosePatternViewController new];
    choosePVC.type = @"sell";
    [self  presentViewController:[[CLNavigationViewController alloc]initWithRootViewController:choosePVC] animated:YES completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)clickBTN:(id)sender {
    
    [self.buyBTCBtn setCircleButtonRadius:10];
    [self.sellBTCBtn setCircleButtonRadius:0];
    self.scroll.contentOffset = CGPointMake(0, 0);
}

- (IBAction)clickBTN1:(id)sender {
    
    [self.buyBTCBtn setCircleButtonRadius:0];
    [self.sellBTCBtn setCircleButtonRadius:10];
    self.scroll.contentOffset = CGPointMake(self.view.frame.size.width, 0);
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
